`timescale 1ns / 1ps

// Create Date: 2019/06/21 16:14:50

`include "aludefines.vh"
`include "defines.vh"

module alu_decoder(
	input [5:0] op_code,
	input [4:0] rt,
	input [5:0] funct,
	output reg [4:0] alu_control
    );
	
	always @* begin
		case(op_code)
			`EXE_R_TYPE: 
					case(funct)
						//算数和逻辑运算
						`EXE_AND:   	alu_control <= `ALU_AND; //1
						`EXE_OR:    	alu_control <= `ALU_OR;
						`EXE_XOR:   	alu_control <= `ALU_XOR;
						`EXE_NOR:   	alu_control <= `ALU_NOR;

						`EXE_ADD:   	alu_control <= `ALU_ADD;	//4
						`EXE_SUB:   	alu_control <= `ALU_SUB;
						`EXE_ADDU:  	alu_control <= `ALU_ADDU;
						`EXE_SUBU:  	alu_control <= `ALU_SUBU;
						`EXE_SLT:   	alu_control <= `ALU_SLT;
						`EXE_SLTU:  	alu_control <= `ALU_SLTU;
							//div and mul
						`EXE_DIV:   	alu_control <= `ALU_SIGNED_DIV;
						`EXE_DIVU:  	alu_control <= `ALU_UNSIGNED_DIV;
						`EXE_MULT:  	alu_control <= `ALU_SIGNED_MULT;
						`EXE_MULTU: 	alu_control <= `ALU_UNSIGNED_MULT;

						//移位指令
						`EXE_SLL:   	alu_control <= `ALU_SLL_SA;	//2
						`EXE_SRL:   	alu_control <= `ALU_SRL_SA;
						`EXE_SRA:   	alu_control <= `ALU_SRA_SA;
						`EXE_SLLV:  	alu_control <= `ALU_SLL;
						`EXE_SRLV:  	alu_control <= `ALU_SRL;
						`EXE_SRAV:  	alu_control <= `ALU_SRA;

						//hilo
						`EXE_MFHI:  	alu_control <= `ALU_MFHI;	//3
						`EXE_MFLO:  	alu_control <= `ALU_MFLO;
						`EXE_MTHI:  	alu_control <= `ALU_MTHI;
						`EXE_MTLO:  	alu_control <= `ALU_MTLO;
						//jump
						`EXE_JR:		alu_control <= `ALU_DONOTHING; //5
						`EXE_JALR:		alu_control <= `ALU_PC_PLUS8;
						default:    	alu_control <= `ALU_ADDU;
					endcase
			//I type
			`EXE_ADDI: 	alu_control <= `ALU_ADD;
			`EXE_ADDIU: alu_control <= `ALU_ADDU;
			`EXE_SLTI: 	alu_control <= `ALU_SLT;
			`EXE_SLTIU: alu_control <= `ALU_SLTU;
			`EXE_ANDI: 	alu_control <= `ALU_AND;
			`EXE_XORI: alu_control <= `ALU_XOR;
			`EXE_LUI: 	alu_control <= `ALU_LUI;
			`EXE_ORI: alu_control <= `ALU_OR;
				//memory
			`EXE_LW, `EXE_LB, `EXE_LBU, `EXE_LH, `EXE_LHU, `EXE_SW, `EXE_SB, `EXE_SH:
						alu_control <= `ALU_ADD;
				//branch and jump
			`EXE_BEQ,`EXE_BGTZ,`EXE_BLEZ,`EXE_BNE:
				alu_control <= `ALU_DONOTHING;
			`EXE_BRANCHS:
				case(rt)
					`EXE_BLTZAL,`EXE_BGEZAL:      
                        alu_control <= `ALU_PC_PLUS8;
                    `EXE_BLTZ, `EXE_BGEZ: 
                        alu_control <= `ALU_DONOTHING;
                    default:
                        alu_control <= `ALU_DONOTHING;
				endcase
			
			//J type
			`EXE_J:		alu_control <= `ALU_DONOTHING;
			`EXE_JAL:	alu_control <= `ALU_PC_PLUS8;
			default:
						alu_control <= `ALU_DONOTHING;
		endcase
	end
endmodule

// `timescale 1ns / 1ps

// // Create Date: 2019/06/21 16:14:50

// `include "aludefines.vh"
// `include "defines.vh"

// module alu_decoder(
// 	input [5:0] op_code,
// 	input [4:0] rt,
// 	input [5:0] funct,
// 	output[4:0] alu_control
//     );
// 	// assign alu_control[5] = (op_code == `EXE_R_TYPE && 
//     //                         (funct == `EXE_AND ||  funct == `EXE_OR || funct == `EXE_XOR 
//     //                             || funct == `EXE_NOR || funct == `EXE_ADD || funct == `EXE_SUB 
//     //                             || funct == `ALU_SLT || funct == `EXE_SLT || funct == `EXE_SLTU 
//     //                             || funct == `EXE_SLL || funct == `EXE_SRL || funct == `ALU_UNSIGNED_MULT 
//     //                             || funct == `ALU_ADDR_JUDGE || funct == `ALU_XOR || funct == `ALU_NOR ))

//     //  仅对1进行处理，通过遍历查看指令宏，对每一条宏对alu_control进行赋值 
// 	assign alu_control[4] =  (op_code == `EXE_R_TYPE && (funct == `EXE_DIVU || funct == `EXE_MULT
//                                 || funct == `EXE_DIV || funct == `EXE_MFHI || funct == `EXE_MTHI
//                                 || funct == `EXE_MFLO || funct == `EXE_MTLO || funct == `EXE_ADDU
//                                 || funct == `EXE_SUBU || funct == `EXE_JALR || funct == `EXE_SLL
//                                 || funct == `EXE_SRL || funct == `EXE_SRA))
//                                 // //do nothing 
//                                 // || funct == `EXE_JR))
                            
//                            ||(op_code == `EXE_BRANCHS && (rt == `EXE_BLTZAL || rt == `EXE_BGEZAL))
//                         //     //    do nothing
//                         //    || rt == `EXE_BLTZ || rt == `EXE_BGEZ))

//                                 //双重判断
//                                 || funct == `EXE_LUI || funct == `EXE_ADDIU || funct == `EXE_JAL;
//                                 // // do nothing 
//                                 // || funct == `EXE_BEQ || fucnt == `EXE_BGTZ || funct == `EXE_BLEZ
//                                 // || funct == `EXE_BNE || funct == `EXE_J

//     assign alu_control[3] =  (op_code == `EXE_R_TYPE && (funct == `EXE_SLLV || funct == `EXE_SRLV
//                                 || funct == `EXE_SRAV || funct == `EXE_SLTU || funct == `EXE_MULTU
//                                 || funct == `EXE_XOR || funct == `EXE_NOR || funct == `EXE_ADDU
//                                 || funct == `EXE_SUBU || funct == `EXE_JALR || funct == `EXE_SLL
//                                 || funct == `EXE_SRL || funct == `EXE_SRA))
//                                 // //do nothing 
//                                 // || funct == `EXE_JR))

//                            ||(op_code == `EXE_BRANCHS && (rt == `EXE_BLTZAL || rt == `EXE_BGEZAL))
//                         //    //    do nothing
//                         //    || rt == `EXE_BLTZ || rt == `EXE_BGEZ))

//                                 //双重判断
//                                 || funct == `EXE_SLTIU || funct == `EXE_XORI || funct == `EXE_ADDIU
//                                 || funct == `EXE_JAL;
//                                 // // do nothing 
//                                 // || funct == `EXE_BEQ || fucnt == `EXE_BGTZ || funct == `EXE_BLEZ
//                                 // || funct == `EXE_BNE || funct == `EXE_J; 

// 	assign alu_control[2] =  (op_code == `EXE_R_TYPE && (funct ==`EXE_SUB || funct == `EXE_SLT 
//                                 || funct == `EXE_MULTU || funct == `EXE_XOR || funct == `EXE_NOR
//                                 || funct == `EXE_MFHI || funct == `EXE_MTHI || funct == `EXE_MFLO 
//                                 || funct == `EXE_MTLO || funct == `EXE_SRL || funct == `EXE_SRA))
//                                 // //do nothing 
//                                 // || funct == `EXE_JR))

//                                 //双重逻辑
//                                 || funct == `EXE_SLTI || funct == `EXE_SLTIU || funct == `EXE_XORI;
//                                 // do nothing 
//                                 // || funct == `EXE_BEQ || fucnt == `EXE_BGTZ || funct == `EXE_BLEZ
//                                 // || funct == `EXE_BNE || funct == `EXE_J; 


// 	assign alu_control[1] =  (op_code == `EXE_R_TYPE && (funct ==`EXE_ADD || funct ==`EXE_SUB || funct == `EXE_SLT 
//                                 || funct == `EXE_SRAV || funct == `EXE_SLTU || funct == `EXE_XOR
//                                 || funct == `EXE_NOR || funct == `EXE_DIV || funct == `EXE_MFLO
//                                 || funct == `EXE_MTLO || funct == `EXE_JALR || funct == `EXE_SLL))
//                                 // //do nothing 
//                                 // || funct == `EXE_JR))

//                            ||(op_code == `EXE_BRANCHS && (rt == `EXE_BLTZAL || rt == `EXE_BGEZAL))
//                         //    //    do nothing
//                         //    || rt == `EXE_BLTZ || rt == `EXE_BGEZ))

//                                 //双重逻辑 
//                                 || op_code == `EXE_ADDI || op_code == `EXE_LW || op_code == `EXE_LB 
//                                 || op_code == `EXE_LBU || op_code == `EXE_LH || op_code == `EXE_LHU 
//                                 || op_code == `EXE_SW || op_code == `EXE_SB || op_code == `EXE_SH 
//                                 || funct == `EXE_SLTI || funct == `EXE_XORI || funct == `EXE_LUI
//                                 || funct == `EXE_JAL;
//                                 // // do nothing 
//                                 // || funct == `EXE_BEQ || fucnt == `EXE_BGTZ || funct == `EXE_BLEZ
//                                 // || funct == `EXE_BNE || funct == `EXE_J;


// 	assign alu_control[0] =  (op_code == `EXE_R_TYPE && (funct == `EXE_OR || funct == `EXE_SLT 
//                                  || funct == `EXE_SRLV || funct == `EXE_SLTU || funct == `EXE_NOR
//                                  || funct == `EXE_MULT || funct == `EXE_MTHI || funct == `EXE_MTLO
//                                  || funct == `EXE_SUBU || funct == `EXE_SLL || funct == `EXE_SRA
//                                 // 尝试修改译码
//                                 || funct == `EXE_AND
//                                  ))
//                                 //  //do nothing 
//                                 // || funct == `EXE_JR)) 

//                                 //双重逻辑
//                                 || (funct == `EXE_ORI) || funct == `EXE_SLTI || funct == `EXE_SLTIU
//                                 || funct == `EXE_LUI; 
//                                 // // do nothing 
//                                 // || funct == `EXE_BEQ || fucnt == `EXE_BGTZ || funct == `EXE_BLEZ
//                                 // || funct == `EXE_BNE || funct == `EXE_J; 
	
// endmodule
